package com.gitee.openviducn.inspector.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.openviducn.inspector.common.ResponseResult;
import com.gitee.openviducn.inspector.constant.AppConstant;
import com.gitee.openviducn.inspector.datasource.entity.OpenviduInfo;
import com.gitee.openviducn.inspector.datasource.mapper.OpenviduInfoMapper;
import com.gitee.openviducn.inspector.domain.vo.request.AddOpenviduInfoRequest;
import com.gitee.openviducn.inspector.domain.vo.request.EditOpenviduInfoRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 04.19 2020
 */
@Slf4j
@Api(tags = "openvidu配置接口")
@RequestMapping(value = "/ovinfo")
@RestController
public class OpenViduInfoController {

    @Resource
    private OpenviduInfoMapper openviduInfoMapper;

    @ApiOperation("所有OpenVidu配置")
    @PostMapping("/all")
    public ResponseResult<List<OpenviduInfo>> all() {
        List<OpenviduInfo> openviduInfos = openviduInfoMapper.selectList(new QueryWrapper<>());
        openviduInfos.forEach(openviduInfo -> openviduInfo.setSecret(AppConstant.SECRET));
        return ResponseResult.success(openviduInfos);
    }

    @ApiOperation("新增OpenVidu配置")
    @PostMapping("/add")
    public ResponseResult<Void> add(@Validated @RequestBody AddOpenviduInfoRequest request) {
        OpenviduInfo openviduInfo = new OpenviduInfo();
        openviduInfo.setHostname(request.getHostname());
        openviduInfo.setSecret(request.getSecret());
        openviduInfoMapper.insert(openviduInfo);
        return ResponseResult.success();
    }

    @ApiOperation("修改OpenVidu配置")
    @PostMapping("/update")
    public ResponseResult<Void> update(@Validated @RequestBody EditOpenviduInfoRequest request) {
        OpenviduInfo openviduInfo = new OpenviduInfo();
        openviduInfo.setId(request.getId());
        openviduInfo.setHostname(request.getHostname());
        openviduInfo.setSecret(request.getSecret());
        openviduInfoMapper.updateById(openviduInfo);
        return ResponseResult.success();
    }

}
